-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
61 lines (44 loc) · 1.29 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n;
while (true) {
n = sc.nextInt();
if (n == 0)
break;
int[] vet = new int[n];
for (int i = 0; i < n; i++)
vet[i] = sc.nextInt();
int maior = obtemMaior(vet);
int indiceSegundoMaior = obtemIndiceSegundoMaior(vet, maior) + 1;
System.out.println(indiceSegundoMaior);
}
sc.close();
}
public static int obtemMaior(int[] vet) {
int maior = vet[0];
for (int i = 0; i < vet.length; i++) {
if (vet[i] > maior) {
maior = vet[i];
}
}
return maior;
}
public static int obtemIndiceSegundoMaior(int[] vet, int maior) {
int segundoMaior = vet[0];
int cont = 0;
if (vet[0] == maior) {
segundoMaior = vet[1];
cont = 1;
}
for (int i = 0; i < vet.length; i++) {
if (vet[i] > segundoMaior && vet[i] != maior) {
segundoMaior = vet[i];
cont = i;
}
}
return cont;
}
}